import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import register from "../pages/register/register.vue"
import login from "../pages/login/login.vue"
import index from "../pages/index/index.vue"
import list from "../pages/list/list.vue"
import detail from "../pages/detail/detail.vue"
import search from "../pages/search/search.vue"


// 二级路由
import home from "../pages/home/home.vue"
import cate from "../pages/cate/cate.vue"
import shop from "../pages/shop/shop.vue"
import mine from "../pages/mine/mine.vue"


const routes = [
  {
    path: "/register",
    component: () => import("../pages/register/register.vue"),
    meta: {
      title: "注册",
      back: true
    }
  },
  {
    path: "/login",
    component: () => import("../pages/login/login.vue"),
    meta: {
      title: "登录",
      register: true
    }
  },
  {
    path: "/index",
    component: () => import("../pages/index/index.vue"),
    children: [
      { path: "home", component: () => import("../pages/home/home.vue"), meta: { title: "首页" } },
      { path: "cate", component: () => import("../pages/cate/cate.vue"), meta: { title: "分类" } },
      { path: "shop", component: () => import("../pages/shop/shop.vue"), meta: { title: "购物车" } },
      { path: "mine", component: () => import("../pages/mine/mine.vue"), meta: { title: "我的" } },
      { path: "/index", redirect: "/index/home" }
    ]
  },
  {
    name:'陈鹏',
    path: "/list",
    component:  () => import("../pages/list/list.vue"),
    meta: {
      title: "商品列表",
      back: true
    }
  },
  {
    path: "/detail",
    component: () => import("../pages/detail/detail.vue"),
    meta: {
      title: "商品详情",
      back: true
    }
  },
  {
    path: "/search",
    component: () => import("../pages/search/search.vue"),
    alias: "/s",
    meta: {
      title: "搜索",
      back: true
    }
  },
  {
    path: "/order",
    component: () => import("../pages/order/order.vue"),
    meta: {
      title: "确认订单",
      back: true
    }
  },
  {
    path: "/pay",
    component: () => import("../pages/pay/pay.vue"),
    meta: {
      title: "订单支付",
      back: true
    }
  },
  {
    path: "/a-list",
    component: () => import("../pages/address_list/list.vue"),
    meta: {
      title: "地址管理",
      back: true
    }
  },
  {
    path: "/a-add",
    component: () => import("../pages/address_add/add.vue"),
    meta: {
      title: "编辑地址",
      back: true
    }
  },
  {
    path: "*",
    redirect: "/login"
  },
]



const router = new VueRouter({
  // 路由模式: hash(默认)[loaclhost:8080/#/login]
  // history-[loaclhost:8080/login]
  mode: "hash",
  // 路由规则
  routes,
  
  // 滚动行为
  // 希望每次进入一个路由，如果是第一次访问过，从(0,0)开始显示
  // 如果之前访问过，就要从上一次离开的位置访问
  /*
      to 前往新的路由信息 $route
      from 从哪里来的路由信息 $route
      savePosition 之前没有访问过这个路由，就是null；如果之前访问，那么他就是之前离开时候的坐标{x:0,y:166}
  */
  scrollBehavior(to, from, savedPosition) {
    // return 返回的是一进来的时候访问过的坐标位置
    return savedPosition ? savedPosition : { x: 0, y: 0 }
  }
})

export default router
